package com.samebuilding.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.samebuilding.domain.Activity;
import com.samebuilding.domain.ActivityBigtype;
import com.samebuilding.domain.User;
import com.samebuilding.service.ActivityService;
import com.samebuilding.service.ActivityTypeService;
import com.samebuilding.util.PaginationUtil;

public class MyActivityAction extends DispatchAction {

	
	private ActivityService activityService;
	private ActivityTypeService activityTypeService;

	
	
	public ActivityTypeService getActivityTypeService() {
		return activityTypeService;
	}



	public void setActivityTypeService(ActivityTypeService activityTypeService) {
		this.activityTypeService = activityTypeService;
	}



	public ActivityService getActivityService() {
		return activityService;
	}



	public void setActivityService(ActivityService activityService) {
		this.activityService = activityService;
	}



	public ActionForward IPost(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) {
		
		User user = (User)request.getSession().getAttribute("user");
		int uid = user.getUserNo();
		int limit = 5;
		
		int cur = 0;
		
		if(request.getParameter("cur") != null) {
			cur = NumberUtils.createInteger(request.getParameter("cur"));
		}
		
		int start = cur * limit;
		
		List<Activity> list = this.activityService.findActivityByPostUid(uid, start, limit);
		int count = this.activityService.findCountByPostUid(uid);
		
		
		List<ActivityBigtype> typeList = this.activityTypeService.findAllBigType();
		
		String url = PaginationUtil.getPaginationUrl(cur, count, limit, "", request);
		
		request.setAttribute("list", list);
		request.setAttribute("url", url);
		request.setAttribute("typeList", typeList);
		
		return mapping.findForward("myActivity");
	}

	
	public ActionForward IJoin(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) {
		
		User user = (User)request.getSession().getAttribute("user");
		int uid = user.getUserNo();
		int limit = 5;
		
		int cur = 0;
		
		if(request.getParameter("cur") != null) {
			cur = NumberUtils.createInteger(request.getParameter("cur"));
		}
		
		int start = cur * limit;
		
		List<Activity> list = this.activityService.findIJoinByUid(uid, start, limit);
		int count = this.activityService.findCountIJoinByUid(uid);
		
		//System.out.println(list.size());
		//System.out.println(count);
		List<ActivityBigtype> typeList = this.activityTypeService.findAllBigType();
		
		String url = PaginationUtil.getPaginationUrl(cur, count, limit, "", request);
		//System.out.println(url);
		request.setAttribute("list", list);
		request.setAttribute("url", url);
		request.setAttribute("typeList", typeList);
		
		return mapping.findForward("IJoin");
	}
	
}
